#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# @File : base_page.py
# @Autor: FY
# @Date: 2021/8/27
from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webdriver import WebDriver

class BasePage:

    _back_list = [(By.ID, "image_cancel"),
                  (By.ID, "")]
    # 指定类型
    def __init__(self, driver: WebDriver):
        self.driver = driver
        self.driver.implicitly_wait(3)

    def find_element(self, locator):
        # 异常判断
        try:
            return  self.driver.find_element(*locator)
        except:
            self.handle_exception()
            return self.find_element(locator)

        return  self.driver.find_element(*locator)

    def find_element_and_click(self, locator):
        try:
            self.find_element(locator).click()
        except:
            self.handle_exception()
            self.find_element(locator).click()




























































































































































































































































































































































































































































































































































































    def handle_exception(self):
        self.driver.implicitly_wait(0)
        # 广告 好评 升级
        for locator in self._back_list:
            elements = self.driver.find_elements(*locator)
            if len(elements) >= 1:
                elements[0].click()
            else:
                print("{} not fount".format(locator))

            page_source = self.driver.page_source
            if "image_cancel" in page_source:
                self.find_element(locator)
            elif "tips" in page_source:
                # 有些tips不用点，点击其它位置
                self.driver.find_element()
                pass

        self.driver.implicitly_wait(3)
